<template>
    <el-container>
        <el-form :model="ruleForm2" :rules="rules2" ref="ruleForm2" label-position="left" label-width="0px"
                 class="demo-ruleForm login-container">
            <h3 class="title">系统登录</h3>
            <el-form-item prop="account">
                <el-input type="text" v-model="ruleForm2.account" auto-complete="on" placeholder="账号"></el-input>
            </el-form-item>
            <el-form-item prop="checkPass">
                <el-input type="password" v-model="ruleForm2.checkPass" auto-complete="on" placeholder="密码"></el-input>
            </el-form-item>
            <el-checkbox v-model="checked" checked class="remember">记住密码</el-checkbox>
            <el-form-item>
                <el-button type="primary" style="width:47%; float: left" @click="showtable">注册</el-button>
                <el-button type="primary" style="width:47%; float:right" @click.native.prevent="login"
                           :loading="logining">登录
                </el-button>
            </el-form-item>
        </el-form>

        <el-dialog :visible.sync="dialogVisible" style="width:800px; margin: 20px auto;">
            <h3 class="dialogTitle">新用户注册</h3>
            <el-form ref="form" :model="form" :rules="rules" label-width="0px" style="width: 90%; margin: 0px auto">
                <el-form-item prop="name">
                    <el-input v-model="form.name" auto-complete="on" placeholder="用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="form.password" auto-complete="on" placeholder="密码"></el-input>
                </el-form-item>
                <el-form-item prop="checkPassword">
                    <el-input type="password" v-model="form.checkPassword" auto-complete="on"
                              placeholder="确认密码"></el-input>
                </el-form-item>
                <el-form-item>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="addUser" style="width:47%; float:left;">确 定
                    </el-button>
                    <el-button type="primary" @click="dialogVisible = false" style="width:47%; float:right;">取 消
                    </el-button>
                </el-form-item>
            </el-form>
        </el-dialog>

    </el-container>
</template>

<script>
    import {requestLogin} from '../api/user';
    import {requestAddUser} from '../api/user';

    export default {
        data() {
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.form.checkPassword !== '') {
                        this.$refs.form.validateField('checkPassword');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.form.password) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                dialogVisible: false,
                logining: false,
                ruleForm2: {
                    account: '',
                    checkPass: ''
                },
                form: {
                    name: '',
                    password: '',
                    checkPassword: '',
                    // role:''
                },
                rules: {
                    name: [
                        {required: true, message: '请输入用户名', trigger: 'blur'},
                    ],
                    password: [
                        {validator: validatePass, trigger: 'blur'}
                    ],
                    checkPassword: [
                        {validator: validatePass2, trigger: 'blur'}
                    ]
                },
                rules2: {
                    account: [
                        {required: true, message: '请输入账号', trigger: 'blur'},
                    ],
                    checkPass: [
                        {required: true, message: '请输入密码', trigger: 'blur'},
                    ]
                },
                checked: true
            };
        },
        methods: {
            showtable() {
                this.dialogVisible = true;
            },
            login() {
                var _this = this;
                this.$refs.ruleForm2.validate((valid) => {
                    if (valid) {
                        this.logining = true;
                        var loginParams = {name: this.ruleForm2.account, password: this.ruleForm2.checkPass};
                        requestLogin(loginParams).then(res => {
                            this.logining = false;
                            let {message, body} = res.data;
                            if (res.status !== 200) {
                                this.$message({
                                    message: message,
                                    type: 'error'
                                });
                            } else {
                                sessionStorage.setItem('user', JSON.stringify(body));
                                this.$router.push({path: '/map'});
                            }
                        });
                    }
                });
            },
            addUser() {
                this.$refs.form.validate((valid) => {
                    if (valid) {
                        var addUserParams = {name: this.form.name, password: this.form.password};
                        requestAddUser(addUserParams).then(res => {
                            let {message, code, body} = res.data;
                            if (res.status !== 200) {
                                this.$message({
                                    message: message,
                                    type: 'error'
                                });
                            } else {
                                window.alert("添加用户成功！");
                                this.dialogVisible = false;
                            }
                        });
                    }
                });
            }


        }
    }

</script>

<style lang="scss" scoped>
    .login-container {
        /*box-shadow: 0 0px 8px 0 rgba(0, 0, 0, 0.06), 0 1px 0px 0 rgba(0, 0, 0, 0.02);*/
        -webkit-border-radius: 5px;
        border-radius: 5px;
        -moz-border-radius: 5px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 35px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;

        .title {
            margin: 0px auto 40px auto;
            text-align: center;
            color: #505458;
        }

        .remember {
            margin: 0px 0px 35px 0px;
        }
    }

    .dialogTitle {
        margin: 0px auto 30px auto;
        text-align: center;
        color: #505458;
    }

    .el-form-item {
        margin-bottom: 18px;
    }

</style>